package com.sname.car.bike.controller;
import com.sname.car.bike.entity.Bike;
import com.sname.car.bike.entity.BikeLocation;
import com.sname.car.bike.service.BikeService;
import com.sname.car.common.constants.Constants;
import com.sname.car.common.exception.CarException;
import com.sname.car.common.resp.ApiResult;
import com.sname.car.common.rest.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created By  醉美柳舞之众星捧月
 * @author songx
 * @date 2018/9/25 16:30
 */
@RestController
@RequestMapping("bike")
public class BikeController extends BaseController {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private BikeService bikeService;
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/25 23:55
     * Description  生成单车
     */
    @RequestMapping("/generateBike")
    public ApiResult generateBike() {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            int result = bikeService.generateBike();
            if (result <= 0) {
                throw new CarException("创建单车失败！");
            }
            resp.setMessage("创建单车成功");
        } catch (Exception e) {
            log.error("Fail to update bike info", e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }
        return resp;
    }
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/25 23:55
     * Description  解锁单车
     */
    @RequestMapping("/unLockBike")
    public ApiResult unLockBike(@RequestBody Bike bike) {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            bikeService.unLockBike(getCurrentUserInformation(), bike.getNumber());
            resp.setMessage("等待单车解锁");
        } catch (CarException e) {
            resp.setCode(e.getStatusCode());
            resp.setSuccess(false);
            resp.setMessage(e.getMessage());
            log.error("Fail to unlock bike", e);
        } catch (Exception e) {
            resp.setMessage("内部错误！");
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            log.error("Fail to unlock bike", e);
        }
        return resp;
    }
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/27 15:04
     * Description  锁定单车
     */
    @RequestMapping("/lockBike")
    public ApiResult lockBike(@RequestBody BikeLocation bikeLocation) {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            bikeService.lockBike(bikeLocation);
            resp.setMessage("锁车成功！");
        } catch (CarException e) {
            resp.setCode(e.getStatusCode());
            resp.setSuccess(false);
            resp.setMessage(e.getMessage());

        } catch (Exception e) {
            log.error("Fail to lock bike ", e);
        }
        return resp;
    }
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/27 16:54
     * Description  单车上报坐标
     */
    @RequestMapping("/reportLocation")
    public ApiResult reportLocation(@RequestBody BikeLocation bikeLocation) {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            bikeService.reportLocation(bikeLocation);
            resp.setMessage("上传坐标成功！");
        } catch (CarException e) {
            resp.setCode(e.getStatusCode());
            resp.setSuccess(false);
            resp.setMessage(e.getMessage());
            log.error("Fail to report location", e);
        }
        return resp;
    }
}
